﻿using System;
using System.Drawing;
using System.IO;
using System.Web;

namespace CourseSystem.Common.Helpers
{
    public class ImgHelper
    {
        

        public static bool IsMatchWidthHeight(HttpPostedFileBase file, int width = 0, int height = 0)
        {
            var img = Image.FromStream(file.InputStream);
            if (width != 0 && img.Width != width)
            {
                return false;
            }
            if (height != 0 && img.Height != height)
            {
                return false;
            }
            return true;
        }

        public static string UploadImg(HttpPostedFileBase file, out string msg)
        {
            return UploadImg(file, ResourcesHelper.UploadImgSize, 0, 0, out msg);
        }

        public static string UploadImg(HttpPostedFileBase file, int max, int width, int height, out string msg)
        {
            msg = "";
            if (file != null && file.ContentLength > 0)
            {
                if (!IsMatchWidthHeight(file, width, height))
                {
                    msg = "只能上传" + width + "*" + height + "的图片.";
                    return "";
                }
                return ResourcesHelper.SaveFile("Img",file,ResourcesHelper.UploadImgExt, max, out msg);
            }
            return "";
        }
    }
}